package Controller;

import Entity.Experiment;
import Service.ExperimentService;
import Service.SelectionService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class BookExpServlet extends HttpServlet {  // 预约
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession(false);
        PrintWriter out =response.getWriter();
        if (session == null) {
            request.getRequestDispatcher("/WEB-INF/page/Login.jsp").forward(request, response);
        }

        SelectionService ss = new SelectionService();
        ExperimentService es = new ExperimentService();

        String uid = (String) session.getAttribute("uid");
        String uname =  (String)session.getAttribute("uname");

        int eid = Integer.parseInt(request.getParameter("eid"));
        //参数
        Experiment e = es.findExperimentById(eid);
        int week = Integer.parseInt(request.getParameter("selweek"));
        int day = Integer.parseInt(request.getParameter("selday"));
        int section = Integer.parseInt(request.getParameter("selsection"));
        String room = request.getParameter("selroom");
        
        System.out.println("实验时间："+week);
        request.setAttribute("uname",uname);
        request.setAttribute("uid",uid);        
        
        if(ss.findByn_e(eid, uid) != null){ 
        	request.getRequestDispatcher("FindRoomServlet").forward(request,response);  //加过实验
        }else{
        	if( ss.addSelection(uid,uname,e,week,day,section,room)) //添加课程
        		request.getRequestDispatcher("AllExpServlet").forward(request,response);
        	else request.getRequestDispatcher("FindRoomServlet").forward(request,response);
        }

    }
}
